
package org.moyakarta.rest;

import java.net.URI;
import java.util.Collection;
import java.util.logging.Logger;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;

@Path("places")
public class PlaceService
{
   
   private static final Logger log =
            Logger.getLogger(PlaceService.class.getName());
   
   @javax.inject.Inject
   private PlaceStorageInGoogleDataStore placeStorageInGoogleDataStore;

   @Path("count")
   @GET
   @Produces("application/json")
   public Response count(@Context UriInfo uriInfo) throws PlaceNotFoundException
   {
      log.info("GET on 'count'");
      int size = placeStorageInGoogleDataStore.countAll();
      return Response.ok(Integer.toString(size)).type("text/plain").status(200).build();
   }
   
   @Path("querycount")
   @GET
   @Produces("application/json")
   public Response queryCount(@QueryParam("fromLat") double fromLat, @QueryParam("toLat") double toLat,
      @QueryParam("fromLng") double fromLng, @QueryParam("toLng") double toLng,
      @QueryParam("sort") String sort) throws PlaceNotFoundException
   {
      log.info("GET on 'querycount' by fromLat,toLat,fromLng,toLng,sort");
      int size = placeStorageInGoogleDataStore.queryCount(fromLat, toLat, fromLng, toLng, sort);
      return Response.ok(Integer.toString(size)).type("text/plain").status(200).build();
   }
   
   @Path("{id}")
   @GET
   @Produces("application/json")
   public Place get(@PathParam("id") String id) throws PlaceNotFoundException
   {
      log.info("GET on '{id}'");
      Place place = placeStorageInGoogleDataStore.getPlace(id);
      if (place == null)
         throw new PlaceNotFoundException(id);
      return place;
   }

   @GET
   @Produces("application/json")
   public Collection<Place> getAll()
   {
      log.info("GET on '' (empty path) ALL");
      // TODO query params
	   int rowsNum = 10;
		int offset = 0;
		return placeStorageInGoogleDataStore.getAll(rowsNum, offset);
   }

   @PUT
   @Consumes("application/json")
   public Response put(Place place, @Context UriInfo uriInfo)
   {
      log.info("PUT on '' (empty path)");
		place.setHash(PlaceGenerateUtils.generateEncodedHash());
//		place.setCreationDate(new java.util.Date());
		
      String id = placeStorageInGoogleDataStore.putPlace(place);
      URI location = uriInfo.getBaseUriBuilder().path(getClass()).path(id).build();
      return Response.created(location).entity(location.toString()).type("text/plain").status(200).build();
   }
   
   @DELETE
   public Response deleteAll()
   {
      log.info("DELETE on '' (empty path) ALL");
      placeStorageInGoogleDataStore.deleteAll();
      return Response.status(200).build();
   }
   
   @Path("{id}")
   @DELETE
   @Produces("application/json")
   public Response delete(@PathParam("id") String id) throws PlaceNotFoundException
   {
      log.info("DELETE on {id}");
      placeStorageInGoogleDataStore.deletePlace(id);
      return Response.status(200).build();
   }
   
}
